package top.jandmla.excel.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.jandmla.excel.bean.*;
import top.jandmla.excel.pojo.QueryPageDTO;
import top.jandmla.excel.pojo.RolePojo;
import top.jandmla.excel.pojo.UserPojo;
import top.jandmla.excel.pojo.UserRolePojo;
import top.jandmla.excel.service.*;
import top.jandmla.excel.util.result.BaseResult;

import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private MenusService menusService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private ShiroService shiroService;


    /**
     * 修改用户角色
     * @param userRolePojo 用户角色
     * @return 改变结果
     */
    @PostMapping("/changeUserRole")
    public BaseResult<String> adminUpdateUserRole(@RequestBody UserRolePojo userRolePojo){
        return userRoleService.changeRole(userRolePojo);
    }

    /**
     * 修改用户基本信息
     * @param user 用户信息
     * @return 改变结果
     */
    @PostMapping("/changeUser")
    public BaseResult<String> adminUpdateUser(@RequestBody User user){
        return userService.updateUserByAdmin(user);
    }

    /**
     *  删除用户的角色
     * @param userRole 用户角色
     * @return 删除结果
     */
    @PostMapping("/dropUserRole")
    public BaseResult<String> adminDelete(@RequestBody UserRole userRole){
        return userRoleService.deleteByUserId(userRole);
    }

    /**
     * 给用户添加角色
     * @param userRole 用户角色信息
     * @return 添加结果
     */
    @PostMapping("/insertUserRole")
    public BaseResult<String> adminInsertUserRole(@RequestBody UserRole userRole){
        return userRoleService.insertUserRole(userRole);
    }

    /**
     * 查询所有权限
     * @param dto 查询条件
     * @return 查询结果
     */
    @PostMapping("/selectMenu")
    public BaseResult<IPage<Menu>> adminSelect(@RequestBody QueryPageDTO<Menu> dto){
        IPage<Menu> result = menusService.selectLit(dto);
        return result != null ? BaseResult.success(result) : BaseResult.fail(1,"查询失败！");
    }

    /**
     * 查询用户以及其角色信息
     * @param dto 插叙你条件
     * @return 查询结果
     */
    @PostMapping("/selectUserAndRole")
    public BaseResult<List<UserPojo>> adminSelectUser(@RequestBody QueryPageDTO<User> dto){
        List<UserPojo> result = userService.getAllUserPojo(dto.getQuery());
        return result.size()>0 ? BaseResult.success(result) : BaseResult.fail(1,"查询失败！");
    }

    /**
     * 新增用户
     * @param user 用户信息
     * @return 新增结果
     */
    @PostMapping("/insertUser")
    public BaseResult<String> adminInsertUser(@RequestBody User user){
        return userService.insertUser(user);
    }

    /**
     * 查询所有用户
     * @return 查询结果
     */
    @PostMapping("/selectAllUser")
    public BaseResult<IPage<User>> adminSelectAllUser(@RequestBody QueryPageDTO<User> dto){
        IPage<User> result = userService.selectAllUserAndPage(dto);
        return result.getRecords().size() > 0 ? BaseResult.success(result) : BaseResult.fail(1,"查询失败！");
    }

    /**
     * 新增权限
     * @param menu 权限信息
     * @return 新增结果
     */
    @PostMapping("/insertMenu")
    public BaseResult<String> adminInsertMenu(@RequestBody Menu menu){
        return menusService.insertMenu(menu);
    }

    /**
     * 删除角色
     * @param role 角色信息
     * @return 删除结果
     */
    @PostMapping("/deleteRole")
    public BaseResult<String> adminDeleteRole(@RequestBody Role role){
        return roleService.deleteRoleById(role);
    }

    /**
     * 新增角色
     * @param role 角色信息
     * @return 新增结果
     */
    @PostMapping("/insertRole")
    public BaseResult<String> adminInsertRole(@RequestBody Role role){
        return roleService.insertRole(role);
    }

    /**
     * 新增角色的权限
     * @param roleMenu 角色权限信息
     * @return 新增结果
     */
    @PostMapping("/insertRoleAndMenu")
    public BaseResult<String> adminInsertRoleAndMenu(@RequestBody RoleMenu roleMenu){
        return roleMenuService.insertRoleAndMenu(roleMenu);
    }

    /**
     * 删除角色权限
     * @param roleMenu 角色权限信息
     * @return 删除结果
     */
    @PostMapping("/deleteRoleAndMenu")
    public BaseResult<String> adminDeleteRoleAndMenu(@RequestBody RoleMenu roleMenu){
        return roleMenuService.deleteRoleAndMenu(roleMenu);
    }

    @PostMapping("reloadMenu")
    public BaseResult<String> adminReloadMenu(){
        return shiroService.updatePermission();
    }

    @PostMapping("getAllRoleAndMenu")
    public BaseResult<List<RolePojo>> getAllRolePojo(@RequestBody Role role){
        List<RolePojo> list =  roleService.getAllRolePojo(role);
        return list.size() > 0 ? BaseResult.success(list) : BaseResult.fail(1,"查询失败,好像没有查到数据！");
    }

}
